package com.example.sqlitedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
MyDBOpenHelper helper;
SQLiteDatabase sqLiteDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper=new MyDBOpenHelper(this);
        sqLiteDatabase=helper.getReadableDatabase();
        addData();
        queryData();
        updateData();
        deleteData();
    }

    private void deleteData() {
        sqLiteDatabase.delete("food","foodname='螺蛳粉'",new String[]{});
    }

    private void updateData() {
        ContentValues newValues = new ContentValues();
        newValues.put("foodname", "萍乡小炒肉");

        sqLiteDatabase.update("food", newValues, "foodname='螺蛳粉'", null);
        //sqLiteDatabase.update("food","","foodname='螺蛳粉'",new String[]{});
    }

    private void queryData() {
        Cursor cursor=sqLiteDatabase.query("food",null,null,null,null,null,null);
        while(cursor.moveToNext()){
            String foodName=cursor.getString(1);//foodname在第二列
            Double foodPrice=cursor.getDouble(2);//foodprice在第二列
            Integer count=cursor.getInt(3);//count在第二列
        }
    }

    private void addData() {
        ContentValues cv=new ContentValues();
        cv.put("foodname","螺蛳粉");
        cv.put("foodprice",30);
        cv.put("count",2);
        sqLiteDatabase.insert("food",null,cv);
    }
}
